package com.example.qianjun.gobang01;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.qianjun.gobang01.bean.Constant;

import java.util.List;

public class SettingPanelActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private Spinner tableScale;
    private Spinner robotPlayerLevel;
    private Spinner noticeRobotPlayerLevel;
    private Spinner chessOrder;
    private Spinner pieceNoticeStyle;
    private Spinner countDownTime;
    private ArrayAdapter<String> arrayAdapter;
    private String[] data;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_panel);


        findViewInit();
        listenerInit();
        showItems();
        showStoreConfig();


    }

    //View初始化
    private void findViewInit() {
        tableScale = (Spinner) findViewById(R.id.tableScale);
        robotPlayerLevel = (Spinner) findViewById(R.id.robotPlayerLevel);
        noticeRobotPlayerLevel = (Spinner) findViewById(R.id.noticeRobotPlayerLevel);
        chessOrder = (Spinner) findViewById(R.id.chessOrder);
        pieceNoticeStyle = (Spinner) findViewById(R.id.pieceNoticeStyle);
        countDownTime = (Spinner) findViewById(R.id.countDownTime);
    }

    //监听初始化
    private void listenerInit() {
        tableScale.setOnItemSelectedListener(this);
        robotPlayerLevel.setOnItemSelectedListener(this);
        noticeRobotPlayerLevel.setOnItemSelectedListener(this);
        chessOrder.setOnItemSelectedListener(this);
        pieceNoticeStyle.setOnItemSelectedListener(this);
        countDownTime.setOnItemSelectedListener(this);
    }

    //初始化显示
    private void showItems() {

        //显示棋盘大小选项
        data = getResources().getStringArray(R.array.tableScale);
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        tableScale.setAdapter(arrayAdapter);


        //显示队以机器人等级
        data = getResources().getStringArray(R.array.robotPlayerLevel);
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        robotPlayerLevel.setAdapter(arrayAdapter);


        //显示提示机器人等级
        data = getResources().getStringArray(R.array.noticeRobotPlayerLevel);
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        noticeRobotPlayerLevel.setAdapter(arrayAdapter);
        //下棋顺序
        data = getResources().getStringArray(R.array.chessOrder);
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        chessOrder.setAdapter(arrayAdapter);
        //落子提示边框
        data = getResources().getStringArray(R.array.pieceNoticeStyle);
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        pieceNoticeStyle.setAdapter(arrayAdapter);
        //倒计时
        data = getResources().getStringArray(R.array.countDown);
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        countDownTime.setAdapter(arrayAdapter);

    }

    //取出设置项，设置显示选项
    private void showStoreConfig() {
        sharedPreferences = getSharedPreferences("gobangConfig", MODE_PRIVATE);

        //取出键盘的设置，并且设置显示
        switch (sharedPreferences.getInt("table_x", Constant.SMALL_CHECKBOARD_X)) {
            case Constant.SMALL_CHECKBOARD_X:
                tableScale.setSelection(0);
                break;
            case Constant.MIDDDLE_CHECKBOARD_X:
                tableScale.setSelection(1);
                break;
            case Constant.LARGE_CHECKBOARD_X:
                tableScale.setSelection(2);
                break;
        }

        //取出对弈机器人级别，并设置显示
        switch (sharedPreferences.getInt("robotPlayerLevel", Constant.ROBOT_LEVEL_FIRST)) {
            case Constant.ROBOT_LEVEL_STUPID:
                robotPlayerLevel.setSelection(0);
                break;
            case Constant.ROBOT_LEVEL_FIRST:
                robotPlayerLevel.setSelection(1);
                break;
            case Constant.ROBOT_LEVEL_SECOND:
                robotPlayerLevel.setSelection(2);
                break;
            case Constant.ROBOT_LEVEL_THIRD:
                robotPlayerLevel.setSelection(3);
                break;
            case Constant.ROBOT_LEVEL_FOURTH:
                robotPlayerLevel.setSelection(4);
                break;
        }
        //取出提示机器人级别，并设置显示
        switch (sharedPreferences.getInt("noticeRobotplayerLevel", Constant.ROBOT_LEVEL_FIRST)) {
            case Constant.ROBOT_LEVEL_STUPID:
                noticeRobotPlayerLevel.setSelection(0);
                break;
            case Constant.ROBOT_LEVEL_FIRST:
                noticeRobotPlayerLevel.setSelection(1);
                break;
            case Constant.ROBOT_LEVEL_SECOND:
                noticeRobotPlayerLevel.setSelection(2);
                break;
            case Constant.ROBOT_LEVEL_THIRD:
                noticeRobotPlayerLevel.setSelection(3);
                break;
            case Constant.ROBOT_LEVEL_FOURTH:
                noticeRobotPlayerLevel.setSelection(4);
                break;
        }
        //取出下棋顺序，并设置显示
        switch (sharedPreferences.getInt("chessOrder", Constant.CHOSE_BLACK_FIRST)) {
            case Constant.CHOSE_BLACK_FIRST:
                chessOrder.setSelection(0);
                break;
            case Constant.CHOSE_WHITE_FIRST:
                chessOrder.setSelection(1);
                break;
            case Constant.CHOSE_BLACK_SECOND:
                chessOrder.setSelection(2);
                break;
            case Constant.CHOSE_WHITE_SECOND:
                chessOrder.setSelection(3);
                break;
        }

        //取出提示边框，并设置显示
        switch (sharedPreferences.getInt("pieceNoticeStyle", Constant.PIECE_NOTICE_STYLE_RECT)) {
            case Constant.PIECE_NOTICE_STYLE_RECT:
                pieceNoticeStyle.setSelection(0);
                break;
            case Constant.PIECE_NOTICE_STYLE_CYCLE:
                pieceNoticeStyle.setSelection(1);
                break;
        }
        //取出倒计时，并设置显示
        switch (sharedPreferences.getInt("countDownTime",Constant.COUNT_DOWN_NUMBER_30)){
            case Constant.COUNT_DOWN_NUMBER_15:
                countDownTime.setSelection(0);
                break;
            case Constant.COUNT_DOWN_NUMBER_20:
                countDownTime.setSelection(1);
                break;
            case Constant.COUNT_DOWN_NUMBER_25:
                countDownTime.setSelection(2);
                break;
            case Constant.COUNT_DOWN_NUMBER_30:
                countDownTime.setSelection(3);
                break;
            case Constant.COUNT_DOWN_NUMBER_40:
                countDownTime.setSelection(4);
                break;
            case Constant.COUNT_DOWN_NUMBER_50:
                countDownTime.setSelection(5);
                break;
            case Constant.COUNT_DOWN_NUMBER_100:
                countDownTime.setSelection(6);
                break;
        }

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        switch (parent.getId()) {
            case R.id.tableScale:
                switch (position) {
                    case 0:
                        editor.putInt("table_x", Constant.SMALL_CHECKBOARD_X);
                        editor.putInt("table_y", Constant.SMALL_CHECKBOARD_Y);
                        break;
                    case 1:
                        editor.putInt("table_x", Constant.MIDDDLE_CHECKBOARD_X);
                        editor.putInt("table_y", Constant.MIDEDLE_CHECKBOARD_Y);
                        break;
                    case 2:
                        editor.putInt("table_x", Constant.LARGE_CHECKBOARD_X);
                        editor.putInt("table_y", Constant.LARGE_CHECKBOARD_Y);
                        break;
                }
                editor.commit();
                break;
            case R.id.robotPlayerLevel:
                switch (position) {
                    case 0:
                        editor.putInt("robotPlayerLevel", Constant.ROBOT_LEVEL_STUPID);
                        break;
                    case 1:
                        editor.putInt("robotPlayerLevel", Constant.ROBOT_LEVEL_FIRST);
                        break;
                    case 2:
                        editor.putInt("robotPlayerLevel", Constant.ROBOT_LEVEL_SECOND);
                        break;
                    case 3:
                        editor.putInt("robotPlayerLevel", Constant.ROBOT_LEVEL_THIRD);
                        break;
                    case 4:
                        editor.putInt("robotPlayerLevel", Constant.ROBOT_LEVEL_FOURTH);
                        break;
                }
                editor.commit();
                break;
            case R.id.noticeRobotPlayerLevel:
                switch (position) {
                    case 0:
                        editor.putInt("noticeRobotplayerLevel", Constant.ROBOT_LEVEL_STUPID);
                        break;
                    case 1:
                        editor.putInt("noticeRobotplayerLevel", Constant.ROBOT_LEVEL_FIRST);
                        break;
                    case 2:
                        editor.putInt("noticeRobotplayerLevel", Constant.ROBOT_LEVEL_SECOND);
                        break;
                    case 3:
                        editor.putInt("noticeRobotplayerLevel", Constant.ROBOT_LEVEL_THIRD);
                        break;
                    case 4:
                        editor.putInt("noticeRobotplayerLevel", Constant.ROBOT_LEVEL_FOURTH);
                        break;
                }
                editor.commit();
                break;
            case R.id.chessOrder:
                switch (position) {
                    case 0:
                        editor.putInt("chessOrder", Constant.CHOSE_BLACK_FIRST);
                        break;
                    case 1:
                        editor.putInt("chessOrder", Constant.CHOSE_WHITE_FIRST);
                        break;
                    case 2:
                        editor.putInt("chessOrder", Constant.CHOSE_BLACK_SECOND);
                        break;
                    case 3:
                        editor.putInt("chessOrder", Constant.CHOSE_WHITE_SECOND);
                        break;
                }
                editor.commit();
                break;
            case R.id.pieceNoticeStyle:
                switch (position) {
                    case 0:
                        editor.putInt("pieceNoticeStyle", Constant.PIECE_NOTICE_STYLE_RECT);
                        break;
                    case 1:
                        editor.putInt("pieceNoticeStyle", Constant.PIECE_NOTICE_STYLE_CYCLE);
                        break;
                }
                editor.commit();
                break;
            case R.id.countDownTime:
                switch (position){
                    case 0:
                        editor.putInt("countDownTime",Constant.COUNT_DOWN_NUMBER_15);
                        break;
                    case 1:
                        editor.putInt("countDownTime",Constant.COUNT_DOWN_NUMBER_20);
                        break;
                    case 2:
                        editor.putInt("countDownTime",Constant.COUNT_DOWN_NUMBER_25);
                        break;
                    case 3:
                        editor.putInt("countDownTime",Constant.COUNT_DOWN_NUMBER_30);
                        break;
                    case 4:
                        editor.putInt("countDownTime",Constant.COUNT_DOWN_NUMBER_40);
                        break;
                    case 5:
                        editor.putInt("countDownTime",Constant.COUNT_DOWN_NUMBER_50);
                        break;
                    case 6:
                        editor.putInt("countDownTime",Constant.COUNT_DOWN_NUMBER_100);
                        break;
                }
                editor.commit();
                break;
        }


    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
